package medical.web.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import medical.database.DbUtil;
import medical.domain.user.User;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
 * 登录验证控制模块
 * @author 演示
 *
 */

public class LoginServlet extends BaseServlet {

	private static final Logger logger = LogManager.getLogger(LoginServlet.class);
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		User user = new User();

		String username = request.getParameter("username");
		String pwd = request.getParameter("pwd");

		if (login(username, pwd)) {
			request.setAttribute("username", username);
			request.setAttribute("pwd", pwd);
			request.getRequestDispatcher("/index2.jsp").forward(request, response);

		} else {
			request.getRequestDispatcher("/login.jsp").forward(request, response);
		}
	}

		private boolean login (String username, String paswword)
		{
			boolean flag = false;
			try {
				Connection con = DbUtil.getConn();
				Statement stmt = con.createStatement();
				ResultSet rs = stmt.executeQuery("select * from t_user where username='" + username + "' and pwd='" + paswword + "'");
				if (rs.next()) {
					flag = true;
				}
				stmt.close();
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return flag;
		}

	}
